package com.atguigu.springcloud.exception.impl;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 自定义异常处理
 * @date 2021/10/12 16:30
 */
public final class CustomAssert {

    private static final int DEFAULT_ERROR = 500;


    public CustomAssert() {

    }

    /**
     * @param cause 如果为false  则抛出异常
     * @param msg   异常提示
     * @param code  状态码
     * @param e     异常类型
     * @return
     * @description: 自定义异常
     */
    public static void notTrue(boolean cause, String msg, int code, Throwable e) {
        System.out.println(cause);
        if (!cause) {
            throw BusinessException.getInstance(msg, code, e);
        }
    }


    //默认500状态码
    public static void notTrue(boolean cause, String msg) {
        notTrue(cause, msg, 500, (Throwable) null);
    }

    //自定义状态码
    public static void notTrue(boolean cause, String msg, int code) {
        notTrue(cause, msg, code, (Throwable) null);
    }


    /**
     * @param cause 参数值是否为null
     * @param msg   异常提示
     * @param code  状态码
     * @param e     异常类型
     * @return
     * @description: 判断值不为null，否则抛出异常
     */
    public static void notNull(Object cause, String msg, int code, Throwable e) {

        notTrue(cause != null, msg, code, e);
    }

    public static void notNull(Object cause, String msg) {
        notNull(cause, msg, 500, (Throwable) null);
    }

    public static void notNull(boolean cause, String msg, int code) {
        notNull(cause, msg, code, (Throwable) null);
    }


    /**
     * @param cause 参数值是否为null
     * @param msg   异常提示
     * @param code  状态码
     * @param e     异常类型
     * @return
     * @description: 去除字符串类型（空字符串为true）的特殊类型
     */
    public static void notBlank(Object cause, String msg, int code, Throwable e) {
        notTrue(cause != null, msg, code, e);
        //去除空字符串为ture
        if (cause instanceof String) {
            notTrue(!cause.equals(""), msg, code, e);
        }
        if (cause instanceof Collection) {
            notTrue(!((Collection) cause).isEmpty(), msg, code, e);
        }
        if (cause instanceof Map) {
            notTrue(!((Map) cause).isEmpty(), msg, code, e);
        }
        if (cause instanceof Set) {
            notTrue(!((Set) cause).isEmpty(), msg, code, e);
        }
    }


    public static void notBlank(Object cause, String msg) {
        notBlank(cause, msg, 500, (Throwable) null);
    }

    public static void notBlank(Object cause, String msg, int code) {
        notBlank(cause, msg, code, (Throwable) null);
    }


    //暂无
    public static void isBlank(Object cause, String msg, int code, Throwable e) {
        notTrue(cause != null, msg, code, e);
        if (cause instanceof String) {
            notTrue(!cause.equals(""), msg, code, e);
        }
        if (cause instanceof Collection) {
            notTrue(!((Collection) cause).isEmpty(), msg, code, e);
        }
        if (cause instanceof Map) {
            notTrue(!((Map) cause).isEmpty(), msg, code, e);
        }
        if (cause instanceof Set) {
            notTrue(!((Set) cause).isEmpty(), msg, code, e);
        }
    }

    public static void isBlank(Object cause, String msg, int code) {
        isBlank(cause, msg, code, (Throwable) null);
    }

    public static void isBlank(Object cause, String msg) {
        isBlank(cause, msg, 500);
    }


    public static void main(String[] args) {
        String data = null;
        notNull(data, "数据为空!!");
    }
}
